import Vue from 'vue'
import moment from 'moment'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')

// 定义全局时间戳过滤器
Vue.filter('datefmt', function (input, pattern = 'YYYY-MM-DD') {
  if (input == null || input.length < 1) return '';
  return moment(input).format(pattern);
});

Vue.filter('moment', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

Vue.filter('money', function(val) {
  if(isNaN(val)) {
    val = "0";
  }

  var scale = 4;
  val = (Math.round(val * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale); //四舍五入保留4位小数
  val = String(val);
  let dot = val.indexOf(".");
  //let decimal = ".00";
  let decimal = val.substr(dot);
  let lastTowDecimal = decimal.substring(3,5);//后两位小数位值
  let lastDecimal = decimal.substring(4,5);//末尾小数位值
  if(lastTowDecimal=="00"){//保留两位小数
    decimal = decimal.substring(0,3);
  }else if(lastDecimal=="0"){//保留三位小数
    decimal = decimal.substring(0,4);
  }
  val = val.substring(0, dot);

  /*
  if(dot != -1) {
    decimal = val.substr(dot);
    val = val.substring(0, dot);
  }
  */

  if(val.length > 3){
    let newArr = [];
    for(let i = 0; i < val.length; i++){
      if((val.length - i) >= 3 && (val.length - i) % 3 == 0 && newArr.length > 0) {
        newArr.push(",");
      }
      newArr.push(val[i]);
    }
    val = newArr.join("");
  }

  // 负数情况
  if (val.indexOf('-,') !== -1) {
    val = val.replace('-,', '-');
  }
  return val + decimal;
})
//两位小数金额显示
Vue.filter('twoBitMoney', function(val) {
  if(isNaN(val)) {
    val = "0";
  }

  var scale = 2;
  val = (Math.round(val * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale); //四舍五入保留2位小数
  val = String(val);
  let dot = val.indexOf(".");
  let decimal = val.substr(dot);
  val = val.substring(0, dot);

  if(val.length > 3){
    let newArr = [];
    for(let i = 0; i < val.length; i++){
      if((val.length - i) >= 3 && (val.length - i) % 3 == 0 && newArr.length > 0) {
        newArr.push(",");
      }
      newArr.push(val[i]);
    }
    val = newArr.join("");
  }

  // 负数情况
  if (val.indexOf('-,') !== -1) {
    val = val.replace('-,', '-');
  }
  return val + decimal;
})

/**
 * 高级money
 */
Vue.filter('exMoney', function(val,limit = false,currency= null) {
  if(limit){
    if(val===-1) return '不限';
  }
  if(val ==='') return '--';
  if(isNaN(val)) return '--';

  var scale = 4;
  val = (Math.round(val * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale); //四舍五入保留4位小数
  val = String(val);
  let dot = val.indexOf(".");
  //let decimal = ".00";
  let decimal = val.substr(dot);
  let lastTowDecimal = decimal.substring(3,5);//后两位小数位值
  let lastDecimal = decimal.substring(4,5);//末尾小数位值
  if(lastTowDecimal=="00"){//保留两位小数
    decimal = decimal.substring(0,3);
  }else if(lastDecimal=="0"){//保留三位小数
    decimal = decimal.substring(0,4);
  }
  val = val.substring(0, dot);

  /*
  if(dot != -1) {
    decimal = val.substr(dot);
    val = val.substring(0, dot);
  }
  */

  if(val.length > 3){
    let newArr = [];
    for(let i = 0; i < val.length; i++){
      if((val.length - i) >= 3 && (val.length - i) % 3 == 0 && newArr.length > 0) {
        newArr.push(",");
      }
      newArr.push(val[i]);
    }
    val = newArr.join("");
  }

  // 负数情况
  if (val.indexOf('-,') !== -1) {
    val = val.replace('-,', '-');
  }
  return currency == null ? val + decimal : currency + val + decimal;
})

Vue.filter('likeName',function(item, filterName, filterVal){
    if(filterVal.trim().length == 0) {
      return item;
    }
    return item.filter((d) => {let name = d[filterName];return filterVal.trim().length > 0 && name.indexOf(filterVal) >= 0})
})
//字数省略显示
Vue.filter('omitWord',function(value, num=10){
  let number = Number(num)
				if (!value) return ''
				if (value.length > number) {
					return value.slice(0, number) + '...'
				}
				return value
})